/// <remarks>
/// WorkSpaceFrm Class
/// This form get the parameters to set or create a Working Folder 
/// in the selected Team Foundation Server Source Control
/// and bind it to a project
/// 
/// Author: Georges Marceau, Matricis Informatique Inc.
/// </remarks>

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.VersionControl.Common;
using VcLib;

namespace VcGUI
{
    public partial class WorkSpaceFrm : Form
    {
        private Logger logger = Logger.Instance;
        private VcLib.TFSFacade VcUtil = VcLib.TFSFacade.Instance;
        private string project;

        public WorkSpaceFrm(string serverName, string project)
        {
            InitializeComponent();

            this.txtServerName.Text = serverName;
            this.project = project;

            this.AcceptButton = this.btnOK;
            this.CancelButton = this.btnCancel;
        }

        private void txtServerName_Leave(object sender, EventArgs e)
        {
            //Refresh the list of Workspaces and Projects when server change
            RefreshInfo();
        }

        private void WorkSpaceFrm_Load(object sender, EventArgs e)
        {
            VcLib.Config config = Config.Instance;
            
            if (!String.IsNullOrEmpty(config.ServerName))
            {
                txtServerName.Text = config.ServerName;
                RefreshInfo();
                if (this.project.Length > 0)
                {
                    lstProjects.SelectedItem = project;
                }
            }
        }

        //Refresh the list of Workspaces and Projects when server change
        void RefreshInfo()
        {
            if (!String.IsNullOrEmpty(txtServerName.Text))
            {
                try
                {
                    this.lstWorkspaces.Items.Clear();
                    this.lstProjects.Items.Clear();
                    Config.Instance.ServerName = txtServerName.Text;
                    Workspace[] wss = VcUtil.GetWorkspaces();

                    // if there's no workspace, create one
                    if(wss.Length == 0)
                    {
                        VcUtil.CreateWorkspace();
                        wss = VcUtil.GetWorkspaces();
                    }
                    foreach (Workspace ws in wss)
                    {
                        this.lstWorkspaces.Items.Add(ws.Name.ToString());
                    }
                    lstWorkspaces.SetSelected(0, true);

                    TeamProject[] tps = VcUtil.GetTeamProjects();
                    foreach (TeamProject tp in tps)
                    {
                        this.lstProjects.Items.Add(tp.Name.ToString());
                    }
                }
                catch (Exception ex)
                {
                    this.logger.Error(ex.Message);
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                //string fullPath;
                // return if there's no selected workingFolder
                if (String.IsNullOrEmpty(txtWFPath.Text))
                {
                    this.logger.Error("Select a local folder source");
                    return;
                }

                // return if there's no selected project
                if(lstProjects.SelectedItems.Count == 0)
                {
                    this.logger.Error("Select a project");
                    return;
                }

                // check if working Folder already exist
                if (!VcUtil.ValidateWorkingFolder(txtWFPath.Text, this.lstWorkspaces.SelectedItem.ToString()))
                {
                    this.logger.Error("There's already a Working folder at that location");
                    this.txtWFPath.Text = "";
                    return;
                }

                // Create working folder and bind it to the selected project
                VcUtil.SetWorkingFolder(lstProjects.SelectedItem.ToString(), txtWFPath.Text, this.lstWorkspaces.SelectedItem.ToString());

                this.logger.Info("Working Folder created");
                ((MainForm)this.ParentForm).PopulateProjectsList();
                this.Close();
            }
            catch(Exception ex)
            {
                this.logger.Error(ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbdlg = new FolderBrowserDialog();
            DialogResult result = fbdlg.ShowDialog();
            if (result == DialogResult.OK)
                txtWFPath.Text = fbdlg.SelectedPath;
        }
    }
}